package com.is.add.util.DimensionUtil;

import java.util.*;

/**
 * 维度数据集
 * Created by liuming on 2016/5/4.
 */
public class DimensionData<T>{
    private LinkedHashMap<String, DimensionData<T>> dataMap=new LinkedHashMap<>();
    private List<T> baseList;
    private List<Dimension<T>> dimensions;
    private int dimensionIndex;
    protected DimensionData(List<Dimension<T>> dimensions){
        this.dimensions=dimensions;
        this.dimensionIndex=0;
        this.baseList=new ArrayList<>();
    }
    protected DimensionData(List<Dimension<T>> dimensions, int dimensionIndex){
        this.dimensions=dimensions;
        this.dimensionIndex=dimensionIndex;
        this.baseList=new ArrayList<>();
    }
    void doStatistic(){
        //维度序列中还有维度尚需统计
        if(dimensionIndex<dimensions.size()){
            //获得当前维度
            Dimension<T> currentDimension=dimensions.get(dimensionIndex);
            //将当前维度数据集中所有BaseBean按当前维度分装到其key对应的维度数据集中
            for(T base:baseList){
                String key=currentDimension.compare(base);
                if(dataMap.containsKey(key)){//命中该key，则添加到key所对应的维度数据集中
                    DimensionData<T> dimensionData=dataMap.get(key);
                    dimensionData.addBase(base);
                }
                else{//未命中则创建该key所对应维度数据集并添加此baseBean
                    DimensionData<T> dimensionData=new DimensionData<>(dimensions,dimensionIndex+1);
                    dimensionData.addBase(base);
                    dataMap.put(key, dimensionData);
                }
            }
            for(DimensionData<T> d:dataMap.values()){
                d.doStatistic();
            }
        }
        /*else{//维度序列中维度已统计完成，该维度数据集中的数据是最终数据

        }*/
    }
    public LinkedHashMap<String, DimensionData<T>> getDataMap() {
        return dataMap;
    }

    public List<T> getBaseList() {
        return baseList;
    }
    private DimensionData addBase(T base) {
        this.baseList.add(base);
        return this;
    }
    DimensionData addBaseList(List<T> baseList) {
        this.baseList.addAll(baseList);
        return this;
    }
}
